//
//  MHOrderListController.m
//  M-Saler
//
//  Created by Teresa on 2020/5/21.
//  Copyright © 2020 Teresa. All rights reserved.
//

#import "MHOrderListController.h"
#import "MHOrderListCell.h"
#import <MJRefresh.h>
#import "MHOrderListRequest.h"
#import "MHOrderInfoModel.h"

@interface MHOrderListController ()
@property(assign,nonatomic)int page;
@property(strong,nonatomic)NSMutableArray<MHOrderInfoModel*>*dataList;

@end

@implementation MHOrderListController
-(NSMutableArray<MHOrderInfoModel *> *)dataList{
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    if (self.dataList.count == 0) {
        [self.tableView.mj_header beginRefreshing];
    }
}

-(void)configUI{
    [MHOrderListCell registerToTableView:self.tableView];
    self.tableView.tableFooterView = [UIView new];
    BLOCKSELF;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [blockSelf fetchHeaderData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [blockSelf fetchFooterData];
    }];
}

-(void)fetchHeaderData{
    self.page = 0;
    [self.dataList removeAllObjects];
    [self.tableView.mj_footer resetNoMoreData];
    self.tableView.mj_footer.hidden = self.dataList.count == 0;
    MHOrderListRequest *req = [MHOrderListRequest new];
    req.hideLoadingView = YES;
    req.page = self.page;
    req.salesman_id = [MHUserModel userModel].theID;
    req.type = (int)self.pageType;
    
    BLOCKSELF;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [blockSelf.tableView.mj_header endRefreshing];
        
        NSArray *arrTemp = response.data;
        if (arrTemp.count) {
            blockSelf.page += 1;
            [blockSelf.dataList addObjectsFromArray:arrTemp];
            [blockSelf.tableView reloadData];
        }
        blockSelf.tableView.mj_footer.hidden = blockSelf.dataList.count == 0;
        
    } failHandler:^(BaseResponse *response) {
        [blockSelf.tableView.mj_header endRefreshing];
        [[HUDHelper sharedInstance]tipMessage:response.message delay:.5];
    }];
}

-(void)fetchFooterData{
    MHOrderListRequest *req = [MHOrderListRequest new];
    req.hideLoadingView = YES;
    req.page = self.page;
    req.salesman_id = [MHUserModel userModel].theID;
    req.type = (int)self.pageType;
    BLOCKSELF;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        
        NSArray *arrTemp = response.data;
        if (arrTemp.count) {
            [blockSelf.tableView.mj_footer endRefreshing];
            blockSelf.page += 1;
            [blockSelf.dataList addObjectsFromArray:arrTemp];
            [blockSelf.tableView reloadData];
        }else{
            [blockSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance]tipMessage:response.message delay:.5];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MHOrderListCell height];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MHOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:MHOrderListCell.nameOfClass forIndexPath:indexPath];
    cell.model = self.dataList[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
